<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: magic_del.php 6752 2010-03-25 08:47:54Z cnteacher $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

if(submitcheck('usesubmit')) {

	if(empty($pid)) {
		showmessage('magics_info_nonexistence');
	}

	$post = getpostinfo($pid, 'pid', array('t.tid', 't.fid', 't.authorid', 'first'));
	checkmagicperm($magicperm['forum'], $post['fid']);

	if($post['authorid'] != $_G['uid']) {
		showmessage('magics_operation_nopermission');
	}

	require_once libfile('function/post');
	$posttable = getposttablebytid($post['tid']);
	if($post['first']) {
		foreach(array('forum_threadmod', 'forum_relatedthread', $posttable, 'forum_poll',
			'forum_polloption', 'forum_trade', 'forum_activity', 'forum_activityapply', 'forum_debate',
			'forum_debatepost', 'forum_attachment', 'forum_typeoptionvar', 'forum_forumrecommend', 'forum_postposition') as $value) {
			DB::query("DELETE FROM ".DB::table($value)." WHERE tid='$post[tid]'", 'UNBUFFERED');
		}

		$query = DB::query("SELECT uid, attachment, dateline, thumb, remote, aid FROM ".DB::table('forum_attachment')." WHERE tid='$post[tid]'");
		while($attach = DB::fetch($query)) {
			dunlink($attach);
		}
		updateforumcount($post['fid']);
	} else {
		DB::query("DELETE FROM ".DB::table($posttable)." WHERE pid='$pid'", 'UNBUFFERED');
		DB::query("DELETE FROM ".DB::table('forum_attachment')." WHERE pid='$pid'", 'UNBUFFERED');
		DB::query("DELETE FROM ".DB::table('forum_attachmentfield')." WHERE pid='$pid'", 'UNBUFFERED');
		$query = DB::query("SELECT uid, attachment, dateline, thumb, remote, aid FROM ".DB::table('forum_attachment')." WHERE pid='$pid'");
		while($attach = DB::fetch($query)) {
			dunlink($attach);
		}
		updatethreadcount($post['tid']);
	}

	usemagic($magicid, $magic['num']);
	updatemagiclog($magicid, '2', '1', '0', '0', $pid);
	showmessage('magics_operation_succeed', '', array('succeed' => 1));

}

function showmagic() {
	global $pid, $lang;
	magicshowtype($lang['option'], 'top');
	magicshowsetting($lang['target_pid'], 'pid', $pid, 'text');
	magicshowtype('', 'bottom');
}

?>